const errcode = require('../utils/errcode')
const utils = require('../utils')
const sqltool = require('sqltool')

exports.createTask = async ctx => {
  const {
    title,
    state,
    remark,
    deadline,
    star,
    collectionId
  } = ctx.request.body

  if (!title) return ctx.error(errcode.REQUIRED_PARAMS)

  const data = utils.pickBy({
    title,
    state,
    remark,
    deadline,
    star
  }, e => typeof e !== 'undefined')

  data.userId = ctx.state.loginId

  const { insertId } = await ctx.models.task.create(data)

  if (collectionId) {
    await ctx.models.taskMap.create({
      taskId: insertId,
      collectionId: collectionId
    })
  }

  ctx.success({
    id: insertId
  })
}

exports.updateTask = async ctx => {
  const {
    id,
    title,
    state,
    remark,
    deadline,
    star
  } = ctx.request.body

  if (!id) return ctx.error(errcode.REQUIRED_PARAMS)

  const data = utils.pickBy({
    title,
    state,
    remark,
    deadline,
    star
  }, e => typeof e !== 'undefined')

  const { affectedRows } = await ctx.models.task.update(data, {
    id: id
  })

  ctx.success({
    affectedRows: affectedRows
  })
}

exports.deleteTask = async ctx => {
  const id = ctx.query.id

  const { affectedRows } = await ctx.models.task.delete({ id: id })

  ctx.success({
    affectedRows
  })
}

exports.getTaskList = async ctx => {
  const collectionId = ctx.query.collectionId
  const loginId = ctx.state.loginId

  const where = {}
  
  if (collectionId) {
    where['b.collectionId'] = collectionId
  }

  const list = await ctx.models.task.findAll({
    attrs: ['a.*', 'b.collectionId'],
    aliasName: 'a',
    joins: [
      { 
        type: 'left join',
        table: ctx.models.taskMap.$name,
        as: 'b',
        on: { 'b.taskId': sqltool.Op.id('a.id'), 'b.status': 1 }
      }
    ],
    where: {
      'a.status': 1,
      'a.userId': loginId
    },
    order: {
      'a.createdAt': 'desc'
    }
  })

  ctx.success({
    list
  })
}

exports.createCollection = async ctx => {
  const {
    title,
    icon,
    parentId,
    sort
  } = ctx.request.body

  if (!title) return ctx.error(errcode.REQUIRED_PARAMS)

  const data = utils.pickBy({
    title,
    icon,
    parentId,
    sort
  }, e => typeof e !== 'undefined')

  data.userId = ctx.state.loginId

  const { insertId } = await ctx.models.collection.create(data)

  ctx.success({
    id: insertId
  })
}

exports.updateCollection = async ctx => {
  const {
    id,
    title,
    icon,
    parentId,
    sort
  } = ctx.request.body

  if (!id) return ctx.error(errcode.REQUIRED_PARAMS)

  const data = utils.pickBy({
    title,
    icon,
    parentId,
    sort
  }, e => typeof e !== 'undefined')

  const { affectedRows } = await ctx.models.collection.update(data, { id: id })

  ctx.success({
    affectedRows
  })
}

exports.deleteCollection = async ctx => {
  const id = ctx.query.id

  const { affectedRows } = await ctx.models.collection.delete({ id: id })

  ctx.success({
    affectedRows
  })
}

exports.getCollectionList = async ctx => {
  const loginId = ctx.state.loginId

  const list = await ctx.models.collection.findAll({
    where: {
      status: 1,
      userId: loginId
    },
    order: {
      sort: 'desc'
    }
  })

  ctx.success({
    list
  })
}
